package com.stud.common.core.domain;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.stud.common.annotation.validator.ValidateInterface;
import com.stud.common.utils.SecurityUtils;
import com.stud.common.utils.StringUtils;
import com.stud.common.utils.Toolkit;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Entity基类
 *
 * @author Stud
 */
@ApiModel
public class BaseEntity implements Serializable, Cloneable, ValidateInterface {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty("主键ID")
    @TableId(type = IdType.AUTO)
    protected Long id;

    @ApiModelProperty("启用状态")
    protected String delFlag = "0";

    @ApiModelProperty("创建者")
    @TableField(fill = FieldFill.INSERT)
    protected String createBy;

    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT)
    protected Date createTime;

    @ApiModelProperty("更新者")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected String updateBy;

    @ApiModelProperty("更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @TableField(fill = FieldFill.INSERT_UPDATE)
    protected Date updateTime;

    @ApiModelProperty("备注")
    protected String remark;

    /**
     * 请求参数
     */
    @TableField(exist = false)
    @JsonIgnore
    protected Map<String, Object> params;


    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(String delFlag) {
        this.delFlag = delFlag;
    }

    public String getCreateBy() {
        return createBy;
    }

    public void setCreateBy(String createBy) {
        this.createBy = createBy;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getUpdateBy() {
        return updateBy;
    }

    public void setUpdateBy(String updateBy) {
        this.updateBy = updateBy;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Map<String, Object> getParams() {
        if (params == null) {
            params = new HashMap<>();
        }
        return params;
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public Object clone() {
        Object obj = null;
        try {
            obj = super.clone();
        } catch (CloneNotSupportedException clonenotsupportedexception) {
        }
        return obj;
    }

    public void initAdd() {
        initAdd(null);
    }

    public void initAdd(String userId) {
        Date now = new Date();

        if (StringUtils.isEmpty(this.getCreateBy()) || StringUtils.isEmpty(this.getUpdateBy())) {
            if (Objects.isNull(userId)) {
                userId = Toolkit.getStringVal(SecurityUtils.getUserIdTry(), null);
            }
        }
        if (StringUtils.isEmpty(this.getDelFlag())) {
            this.setDelFlag("0");
        }
        if (StringUtils.isEmpty(this.getCreateBy())) {
            this.setCreateBy(userId);
        }
        if (StringUtils.isEmpty(this.getUpdateBy())) {
            this.setUpdateBy(userId);
        }
        if (Objects.isNull(this.getCreateTime())) {
            this.setCreateTime(now);
        }
        if (Objects.isNull(this.getUpdateTime())) {
            this.setUpdateTime(now);
        }
    }
}
